﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CuaHangMayTinh.BusinessLogicLayer;
using CuaHangMayTinh.Utilities;

namespace CuaHangMayTinh.PresentationLayer
{
    public partial class FormDoanhThu : Form
    {
        ///công thức tính doanh thu = tong tien đầu vao - tổng tiền đầu ra 


        DataTable dtban = BanHangManager.Find_All(true);
        DataTable dtnhap = NhapHangManager.Find_All(true);
        DataTable dttrahang = XuatKhoTraHangManager.Find_All(true);
        DataTable dtbaohanh = BaoHanhSuaChuaManager.Find_All(false);

        /// <summary>
        /// kiểu tìm kiếm của hoa don
        /// </summary>
        enum KieuTimKiem
        {
            Ngày,
            Tháng,
            Năm,
            Quý
        }

        /// <summary>
        /// table chứa thông tin tìm kiếm
        /// </summary>
        private DataTable Table { get; set; }

        /// <summary>
        /// phương thức khởi tạo
        /// </summary>
        public FormDoanhThu()
        {
            InitializeComponent();
        }

        /// <summary>
        /// băt sự kiện load của hóa đơn bán hàng
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormDoanhThu_Load(object sender, EventArgs e)
        {
            //            Table = doanhthu.Find_All(true);
            LoadListView(Table);
            toolStripcbb_kieu.ComboBox.DataSource = Enum.GetValues(typeof(KieuTimKiem));
        }


        /// <summary>
        /// load lên datagriview từ dt
        /// </summary>
        /// <param name="dt"></param>
        private void LoadListView(DataTable dt)
        {
            dataGridViewX_Select.DataSource = DataUtil.ChangeColumn(dt);
            if (dt != null)
            {
                dataGridViewX_Select.Columns[0].Visible = false;
                dataGridViewX_Select.Columns[4].DefaultCellStyle.Format = "dd/MM/yy  h:mm:ss tt";
            }
        }

        private DataTable GetTable(DataTable tbTable, string text, int kieu)
        {
            if (StringHelper.IsNullorEmpty(text))
            {
                LoadListView(Table);
                return null;
            }
            DataTable dt = new DataTable();
            switch (kieu)
            {
                case 0:
                    dt = DataUtil.TimkiemTheo(tbTable, "NgayLap", int.Parse(text), 0);
                    break;
                case 1:
                    dt = DataUtil.TimkiemTheo(tbTable, "NgayLap", int.Parse(text), 1);
                    break;
                case 2:
                    dt = DataUtil.TimkiemTheo(tbTable, "NgayLap", int.Parse(text), 2);
                    break;
                case 3:
                    dt = DataUtil.TimkiemTheo(tbTable, "NgayLap", int.Parse(text), 3);
                    break;
                default:
                    break;
            }
            return dt;
        }

        /// <summary>
        /// bắt sự kiện thay đổi text search 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripTextBox_serchkey_TextChanged(object sender, EventArgs e)
        {
            string text = (sender as ToolStripTextBox).Text;
            if (StringHelper.IsNullorEmpty(text))
            {
                LoadListView(Table);
                return;
            }
            int kieu = (int)Enum.Parse(typeof(KieuTimKiem), toolStripcbb_kieu.SelectedItem.ToString());
            DataTable dt = new DataTable();
            DataTable talenhap = GetTable(dtnhap, text, kieu);
            DataTable tableban = GetTable(dtban, text, kieu);
            DataTable taletrahang = GetTable(dttrahang, text, kieu);
            DataTable tablebaohanh = GetTable(dtbaohanh, text, kieu);
            decimal tongtiennhap = 0;
            decimal tongtienban = 0;
            decimal tongtientrahang = 0;
            decimal tongtienbaohanh = 0;
            if (talenhap != null)
                tongtiennhap = talenhap.AsEnumerable().Select(x => x.Field<decimal>("TongTien")).Sum();
            if (tableban != null)
                tongtienban = tableban.AsEnumerable().Select(x => x.Field<decimal>("TongTien")).Sum();
            if (taletrahang != null)
                tongtientrahang = taletrahang.AsEnumerable().Select(x => x.Field<decimal>("TongTien")).Sum();
            if (tablebaohanh != null)
                tongtienbaohanh = tablebaohanh.AsEnumerable().Select(x =>
                {
                    try
                    {
                        return x.Field<decimal>("TongTien");
                    }
                    catch (Exception)
                    {
                        return 0;
                    }
                }).Sum();
            decimal doanhthu = tongtienban + tongtienbaohanh + tongtientrahang - tongtiennhap;
            dt.Columns.Add("LoaiDoanhThu");
            dt.Columns.Add("DoanhThu");
            dt.Rows.Add(new[]
            {
                "Doanh thu " + toolStripcbb_kieu.ComboBox.SelectedValue +" "+ text,
                doanhthu.ToString()
            });
            dataGridViewX_Select.DataSource = dt;
        }
    }
}
